#include"List.h"
Listnode* ListInit()
{
	Listnode* phead = BuyList(0);
	phead->next = phead;
	phead->prev = phead;

	return phead;
}

Listnode* BuyList(ListType x)
{
	Listnode* newnode = (Listnode*)malloc(sizeof(Listnode));
	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;

	return newnode;
}

void ListPrint(Listnode* phead)
{
	Listnode* tail = phead->next;
	while (tail != phead)
	{
		printf("%d ", tail->data);
		tail = tail->next;
	}
	printf("\n");
}

void ListPushFront(Listnode* phead, ListType x)
{
	assert(phead);
	Listnode* newnode = BuyList(x);
	Listnode* tail = phead->next;
	newnode->next = tail;
	tail->prev = newnode;
	newnode->prev = phead;
	phead->next = newnode;
}

void ListPushBack(Listnode* phead, ListType x)
{
	assert(phead);
	Listnode* tail = phead->prev;
	Listnode* newnode = BuyList(x);
	newnode->next = phead;
	phead->prev = newnode;
	newnode->prev = tail;
	tail->next = newnode;
}

void ListPopBack(Listnode* phead)
{
	assert(phead != NULL);
	assert(phead->next != phead);
	Listnode* tail = phead->prev;
	Listnode* prev = tail->prev;
	phead->prev = prev;
	prev->next = phead;
	free(tail);
	tail = NULL;
}

void ListPopFront(Listnode* phead)
{
	assert(phead != NULL);
	assert(phead->next != phead);
	Listnode* tail = phead->next;
	Listnode* prev = tail->next;
	phead->next = prev;
	prev->prev = phead;
	free(tail);
	tail = NULL;
}

Listnode* ListFind(Listnode* phead, ListType x)
{
	assert(phead);
	Listnode* tail = phead->next;
	while (tail != phead)
	{
		if (tail->data == x)
		{
			return tail;
		}
		tail = tail->next;
	}
	return NULL;
}

void ListInsert(Listnode* pos, ListType x)
{
	assert(pos);
	Listnode* newnode = BuyList(x);
	Listnode* tail = pos->prev;
	newnode->next = pos;
	pos->prev = newnode;
	tail->next = newnode;
	newnode->prev = tail;
}

void ListErase(Listnode* pos)
{
	assert(pos);
	Listnode* prev = pos->prev;
	Listnode* next = pos->next;
	prev->next = next;
	next->prev = prev;
	free(pos);
	pos = NULL;
}